package com.archeanx.libx.adapter.divider;


import android.content.Context;

import androidx.annotation.ColorRes;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.archeanx.libx.adapter.R;
import com.archeanx.libx.adapter.XRvStatusAdapter;
import com.archeanx.libx.adapter.divider.util.XDividerItemDecoration;
import com.archeanx.libx.adapter.divider.util.XDivider;
import com.archeanx.libx.adapter.divider.util.XDividerBuilder;


/**
 * @author xcoder_xz
 * @date 8017/5/1 0001
 * @link XRvVerticalDivider
 */

@Deprecated
public class XRvVerDivider extends XDividerItemDecoration {
    private int mColor;
    private int mWidthDp;
    private int mStartPadding;
    private int mEndPadding;
    private boolean isShowTopLine;
    private XDivider divider1;
    private XDivider divider2;
    private XDivider divider3;
    private boolean isShowBottomLine;
    private boolean isSingleItemShowBottomLine;

    private XRvVerDivider(Context context, int color, int widthDp, int startPadding, int endPadding, boolean showTopLine, boolean showBottomLine, boolean singleItemShowBottomLine) {
        super(context);
        this.mColor = ContextCompat.getColor(context, color);
        this.mWidthDp = widthDp;
        this.mStartPadding = startPadding;
        this.mEndPadding = endPadding;
        this.isShowTopLine = showTopLine;
        this.isShowBottomLine = showBottomLine;
        this.isSingleItemShowBottomLine = singleItemShowBottomLine;
    }

    @Override
    public XDivider getDivider(RecyclerView.Adapter adapter, int itemPosition) {
        //当只有一个item并且item是状态item 时，不显示分割线
        if (adapter != null && adapter.getItemCount() == 1 && adapter instanceof XRvStatusAdapter && ((XRvStatusAdapter) adapter).isShowStatusLayout()) {
            if (divider3 == null) {
                divider3 = new XDividerBuilder().create();
            }
            return divider3;
        } else if (itemPosition == 0 && isShowTopLine) {
            //是否显示顶部分割线
            if (divider1 == null) {
                divider1 = new XDividerBuilder()
                        .setTopSideLine(true, mColor, mWidthDp, mStartPadding, mEndPadding)
                        .setBottomSideLine(true, mColor, mWidthDp, mStartPadding, mEndPadding)
                        .create();
            }
            return divider1;
        } else if (!isShowBottomLine && adapter != null && adapter.getItemCount() == itemPosition + 1) {
            //不显示底部分割线
            if (divider3 == null) {
                divider3 = new XDividerBuilder().create();
            }
            return divider3;
        } else if (!isSingleItemShowBottomLine && adapter != null && adapter.getItemCount() == 1) {
            //单个item时，不显示底部分割线
            if (divider3 == null) {
                divider3 = new XDividerBuilder().create();
            }
            return divider3;
        } else {
            if (divider2 == null) {
                divider2 = new XDividerBuilder()
                        .setBottomSideLine(true, mColor, mWidthDp, mStartPadding, mEndPadding)
                        .create();
            }
            return divider2;
        }
    }


    public static class Builder {
        /**
         * 分割线颜色 resouce
         */
        private int mColor;
        /**
         * 分割线高度
         */
        private int mWidthDp = 1;
        /**
         * 分割线左边padding
         */
        private int mStartPadding = 0;
        /**
         * 分割线右边padding
         */
        private int mEndPadding = 0;
        /**
         * 是否显示顶部分割线
         */
        private boolean isShowTopLine = false;
        private Context mContext;
        /**
         * 是否显示底部分割线
         */
        private boolean isShowBottomLine = true;

        /**
         * 是否在只有一个itme时，显示底部分割线
         */
        private boolean isSingleItemShowBottomLine = true;


        public Builder(Context context) {
            this.mContext = context;
            mColor = R.color.app_divider_color;
        }

        /**
         * 是否在只有一个itme时，显示底部分割线
         *
         * @param isShow true 显示【默认】，
         * @return
         */
        public Builder setSingleItemShowBottomLine(boolean isShow) {
            isSingleItemShowBottomLine = isShow;
            return this;
        }

        /**
         * 设置分割线颜色
         */
        public Builder setLineColorResources(@ColorRes int color) {
            this.mColor = color;
            return this;
        }

        /**
         * 设置线条宽度/高度
         */
        public Builder setLineWidthDp(int widthDp) {
            this.mWidthDp = widthDp;
            return this;
        }

        /**
         * 设置开始边距
         */
        public Builder setStartPadding(int startPadding) {
            this.mStartPadding = startPadding;
            return this;
        }

        /**
         * 设置结束边距
         */
        public Builder setEndPadding(int endPadding) {
            this.mEndPadding = endPadding;
            return this;
        }

        public Builder setPadding(int startPadding, int endPadding) {
            this.mStartPadding = startPadding;
            this.mEndPadding = endPadding;
            return this;
        }

        /**
         * 是否显示第一行的分割线
         */
        public Builder setShowTopLine(boolean showTopLine) {
            this.isShowTopLine = showTopLine;
            return this;
        }

        /**
         * 是否显示最后一跟分割线
         *
         * @param showBottomLine 默认 显示
         * @return this
         */
        public Builder setShowBottomLine(boolean showBottomLine) {
            isShowBottomLine = showBottomLine;
            return this;
        }

        public XRvVerDivider build() {
            return new XRvVerDivider(mContext, mColor, mWidthDp, mStartPadding, mEndPadding, isShowTopLine, isShowBottomLine, isSingleItemShowBottomLine);
        }

    }
}